package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.entities.login.UserInfo;
import com.maiji.cloud.microservice.UserInfoService;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.ResMetaData;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.ArticleCommentListResData;
import com.maiji.cloud.response.login.UserInfoResData;
import com.maiji.cloud.service.AppUserService;
import com.maiji.cloud.utils.BaseService;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.login.ArticleCommentEntity;
import com.maiji.cloud.mapper.ArticleCommentMapper;
import com.maiji.cloud.service.ArticleCommentService;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleCommentEntity> implements ArticleCommentService {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private AppUserService appUserService;

    @Override
    public BaseMetaResDto<List<ArticleCommentListResData>> findAllArticleComments(String articleId, String token, ReqMetaData metaData) {
        AppUser appUser = null;
        if (StringUtil.isNotBlank(token)) appUser = appUserService.checkToken(token);
        EntityWrapper<ArticleCommentEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("article_id", articleId);
        entityWrapper.eq("is_del", 0);
        entityWrapper.eq("type", 0);
        entityWrapper.orderBy("in_time", false);
        Page<ArticleCommentEntity> page = selectPage(new Page<>(metaData.getPage(), metaData.getSize()), entityWrapper);
        List<ArticleCommentListResData> resDataList = Lists.newArrayList();
        if (page.getRecords().size() > 0) {
            List<String> userIds = page.getRecords().parallelStream().map(ArticleCommentEntity::getSourceUserId).collect(Collectors.toList());
            resDataList = BaseService.dealWithOneToOne(page.getRecords(),"getSourceUserId", ArticleCommentListResData.class,
                    "setSourceUser", userInfoService.selectList(userIds),"getUserId", UserInfoResData.class);
            AppUser finalAppUser = appUser;
            if (finalAppUser != null) resDataList = resDataList.parallelStream().map(resData -> resData.setIsMine(resData.getSourceUserId().equals(finalAppUser.getUuId()))).collect(Collectors.toList());
            List<String> acIds = page.getRecords().parallelStream().map(ArticleCommentEntity::getUuId).collect(Collectors.toList());
            EntityWrapper<ArticleCommentEntity> wrapper = new EntityWrapper<>();
            wrapper.eq("article_id", articleId);
            wrapper.eq("is_del", 0);
            wrapper.ne("type", 0);
            wrapper.in("belong_id", acIds);
            wrapper.orderBy("in_time", false);
            List<ArticleCommentEntity> accList = selectList(wrapper);
            if (accList.size() > 0) {
                userIds = accList.parallelStream().map(ArticleCommentEntity::getSourceUserId).collect(Collectors.toList());
                List<ArticleCommentListResData> accs = BaseService.dealWithOneToOne(accList,"getSourceUserId", ArticleCommentListResData.class,
                       "setSourceUser", userInfoService.selectList(userIds),"getUserId", UserInfoResData.class);
                userIds = accList.parallelStream().map(ArticleCommentEntity::getTargetUserId).collect(Collectors.toList());
                accs = BaseService.dealWithOneToOne(accs,"getTargetUserId", ArticleCommentListResData.class,
                       "setTargetUser", userInfoService.selectList(userIds),"getUserId", UserInfoResData.class);
                if (finalAppUser != null) accs = accs.parallelStream().map(resData -> resData.setIsMine(resData.getSourceUserId().equals(finalAppUser.getUuId()))).collect(Collectors.toList());
                Map<String, List<ArticleCommentListResData>> accsMap = accs.parallelStream().collect(Collectors.groupingBy(ArticleCommentListResData::getBelongId));
                resDataList.parallelStream().forEach(resData -> {
                    List<ArticleCommentListResData> accResDatas = accsMap.get(resData.getUuId());
                    if (accResDatas == null) accResDatas = Lists.newArrayList();
                    resData.setList(accResDatas).setReplySumCount(accResDatas.size());
                });
            }
        }
        ResMetaData resMetaData = new ResMetaData(page.getCurrent(), page.getSize(), page.getTotal());
        return new BaseMetaResDto<List<ArticleCommentListResData>>(Status.SUCCESS).setData(resDataList).setResMetaData(resMetaData);
    }

    @Override
    public BaseResDto delArticleComments (List<String> commentIds) {
        List<ArticleCommentEntity> articleCommentList = commentIds.parallelStream().map(commentId ->
                new ArticleCommentEntity().setUuId(commentId).setIsDel(1)).collect(Collectors.toList());
        if (updateBatchById(articleCommentList)) return new BaseResDto(Status.SUCCESS);
        return new BaseResDto(Status.ERROR);
    }

}
